package com.userfactssample.client.client.framework;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.smartgwt.mobile.client.widgets.Canvas;

public abstract class UMComponent<T extends Canvas> {
	
	protected T uiComponent;
	protected boolean initialised = false;
	
	public static String IMAGE_LOC = GWT.getModuleBaseURL() + "images/";

	public UMComponent(T uiComponent) {
		this.uiComponent = uiComponent;
	}

	public UMComponent<T> getUMComponent() {
		return this;
	}
	
	public T getComponent() {
		return (T) getUiComponent();
	}
	
	
	public T getUiComponent() {
		if (!initialised) {
			initialised = true;
			initialiseComponent();
		}

		return (T) uiComponent;
	}
	
	public void setSize(String width, String height) {
		if (width != null)
			uiComponent.setWidth(width);
		else
			setWidth100();

		if (height != null)
			uiComponent.setHeight(height);
		else
			setHeight100();
	}
	
	public void setWidth100(){
		setWidth("100%");
	}
	
	public void setHeight100(){
		setHeight("100%");
	}
	
	public void setWidth(String width) {
		if (width != null)
			uiComponent.setWidth(width);
	}	
	
	public void setHeight(String height) {
		if (height != null)
			uiComponent.setHeight(height);
	}
	
	public void setWidth(Integer width) {
		if (width != null)
			uiComponent.setWidth(width);
	}
	
	public void setHeight(Integer height) {
		if (height != null)
			uiComponent.setHeight(height);
	}
	
	public void setMargin(Integer margin) {
		if (margin != null)
			uiComponent.setMargin(margin);
	}
	
	public Integer getMargin() {
		return uiComponent.getMargin();
	}
	
	public void setVisible(boolean visible) {
		uiComponent.setVisible(visible);
	}
	
	public Boolean isVisible(){
		return uiComponent.isVisible();
	}
	
	public Integer getHeight() {
		return this.uiComponent.getHeight();
	}
	
	public Integer getWidth() {
		return this.uiComponent.getWidth();
	} 
	
	public Integer getOffsetWidth() {
		return this.uiComponent.getOffsetWidth();
	}
	
	public Integer getOffsetHeight() {
		return this.uiComponent.getOffsetHeight();
	}
	
	public Style getStyle(){
		return uiComponent.getElement().getStyle();
	}
	
	public void setPosition(Position style){
		getStyle().setPosition(Position.ABSOLUTE);
	}
	
	public void setTop(int px) {
		getStyle().setTop(px, Unit.PX);
	}	
	
	public void clear() {
		uiComponent.clear();
	}
	
	public void setTitle(String title) {
		uiComponent.setTitle(title);
	}

	public void disable() {
		uiComponent.disable();
	}
	
	public void enable(){
		uiComponent.enable();
	}
	
	public boolean isIPhone(){
		return Canvas.isIPhone();
	}
	
	public boolean isStandAlone(){
		return uiComponent.isStandAlone();
	}
	
	public void sinkEvents(int eventBitsToAdd){
		uiComponent.sinkEvents(eventBitsToAdd);
	}
	
	protected abstract void initialiseComponent();
}
